package com.ipet.app.adapter.ViewHolder;

import android.animation.ObjectAnimator;
import android.support.annotation.LayoutRes;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ipet.app.R;
import com.ipet.app.ui.widget.FlowLayout;
import com.ipet.app.ui.widget.MyScrollView;
import com.ipet.app.util.AniMateUtil;
import com.ipet.app.util.RippleHelper;
import com.ipet.app.util.TDevice;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;

/**
 * Created by jindongping on 16/4/14.
 */
public class FragmentTwoCenterViewHolder extends BaseViewHolder<String>{
    private MyScrollView discoverScrollView;
    private RelativeLayout discoverMore;
    private ImageView discoverArrow;
    private TextView discoverTv;

    public FragmentTwoCenterViewHolder(ViewGroup parent, @LayoutRes int res) {
        super(parent, res);
        discoverScrollView =$(R.id.discover_center);
        discoverMore=$(R.id.discover_more);
        discoverArrow =$(R.id.discover_bottom_arrow);
        discoverTv =$(R.id.discover_center_tv);
    }


    private boolean isOpened;
    private final String[] mDatas = {"太子妃升职记", "奶酪陷阱", "拜托了冰箱", "吃货天下", "请和废材的我谈恋爱", "暴走大事件", "了不起的挑战",
            "太子妃升职记", "奶酪陷阱", "拜托了冰箱", "吃货天下", "请和废材的我谈恋爱", "暴走大事件", "了不起的挑战",
            "太子妃升职记", "奶酪陷阱", "拜托了冰箱", "吃货天下", "请和废材的我谈恋爱", "暴走大事件", "了不起的挑战"};

    @Override
    public void setData(String data) {
        super.setData(data);

        FlowLayout mLayout = new FlowLayout(getContext());
        discoverScrollView.addView(mLayout);

        ViewGroup.LayoutParams layoutParams = discoverScrollView.getLayoutParams();
        layoutParams.height = TDevice.dipToPx(getContext(), 80);
        discoverScrollView.setLayoutParams(layoutParams);

        mLayout.setSpace( TDevice.dipToPx(getContext(), 12), TDevice.dipToPx(getContext(), 12));

        discoverMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isOpened) {
                    ObjectAnimator.ofFloat(discoverArrow, "rotation", 0, 180).start();
                    AniMateUtil.animateHeight(TDevice.dipToPx(getContext(), 80), TDevice.dipToPx(getContext(), 200), discoverScrollView);
                    discoverTv.setText("收起");
                    //设置可滑动
                    discoverScrollView.setCanScroll(true);
                } else {
                    ObjectAnimator.ofFloat(discoverArrow, "rotation", -180, 0).start();
                    AniMateUtil.animateHeight(TDevice.dipToPx(getContext(), 200), TDevice.dipToPx(getContext(), 80), discoverScrollView);
                    discoverTv.setText("查看更多");
                    //设置不可滑动
                    discoverScrollView.setCanScroll(false);
                }
                isOpened = !isOpened;
            }
        });

        for (int i = 0; i < mDatas.length; i++) {

            LinearLayout linearLayout = new LinearLayout(getContext());
            TextView tv = new TextView(getContext());
            tv.setText(mDatas[i]);
            tv.setPadding( TDevice.dipToPx(getContext(), 5),  TDevice.dipToPx(getContext(), 5),  TDevice.dipToPx(getContext(), 5),  TDevice.dipToPx(getContext(), 5));
            linearLayout.addView(tv);
            linearLayout.setBackgroundResource(R.color.white);
            RippleHelper.addRipple(getContext(),tv);

            final int index = i;
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getContext(), "" + mDatas[index], Toast.LENGTH_SHORT).show();
                }
            });

            mLayout.addView(linearLayout);
        }


    }
}
